<?php
include "./head.php";

//判断是否提交
if($_POST['user']){
    //1.判断合法性
    if(empty($_POST['pass'])||empty($_POST['pass2'])||empty($_POST['nickname'])||empty($_POST['yzm'])){
        exit("<script>alert('请填写完整数据！');location.href='./zc.php'</script>");
    }

    if($_POST['pass']!=$_POST['pass2']){
        exit("<script>alert('密码不一致！');location.href='./zc.php'</script>");
    }

    if($_POST['yzm']!=$_SESSION['zc_yzm']){

        exit("<script>alert('验证码不正确！');location.href='./zc.php'</script>");
    }

    //判断用户名是否存在
    $cont=db_sel("select count(*) as co from bl_member where user = '{$_POST['user']}'");
    if($cont['co']>0){
        exit("<script>alert('用户名已经存在！');location.href='./zc.php'</script>");
    }

    //接收存入数据库
    $arr=array();
    $arr['user']=$_POST['user'];
    $arr['pass']=md5($_POST['pass']);
    $arr['nickname']=$_POST['nickname'];
    $arr['ip']=$_SERVER['REMOTE_ADDR']; //PHP获取客服端ip
    $arr['time']=time();

    if(db_insert('bl_member',$arr)){
        exit("<script>alert('注册成功');location.href='./login.php'</script>");
    }else{
        exit("<script>alert('注册失败！');location.href='./zc.php'</script>");
    }
}
?>
<style>
    .box{
        width: 70%;
        margin: 20px auto;
        border:1px rgba(232, 232, 232, 0.94) solid;
    }
    .cont{
        text-align: center;
        margin-top: 30px;
        font-size:16px;
    }
    .cont input{
        height: 20px;
        border: 1px #ccc solid;
        border-radius: 4px;
    }
    .dl{
        border-radius: 4px;
        width: 80%;
        background: red;
        font-size: 18px;
        color: #fff;
    }
    .yzm{
        margin-left: 40%;
        width: 126px;
    }
</style>
<div class="box">
    <h2 style="text-align: center;border-bottom: 1px rgba(232, 232, 232, 0.94) dashed;padding-bottom: 10px">
        <span style="display: inline-block;width: 50px;float: left;"> < </span>
        会员注册
    </h2>
    <form action="./zc.php" method="post">
        <div class="cont">帐号: <input type="text" name="user"></div>
        <div class="cont">密码: <input type="text" name="pass"></div>
        <div class="cont">确定密码: <input type="text" name="pass2"></div>
        <div class="cont">昵称: <input type="text" name="nickname"></div>

        <div class="cont">
            验证码:<input type="text" name="yzm"/>
            <img src="./yzm.php?type=zc" id="yzm" alt="" class="yzm"/>
            <a onclick="return yzm()">看不清楚换一种</a>

        </div>
        <div class="cont"> <input type="submit" value="注册" class="dl" style="height: 50px;  border: 0px;    border-radius: 8px"></div>

        <div style="margin-top: 10px;text-align: right;"><a href="./login.php">已有账号去登录！</a></div>
    </form>
</div>
<script>
    function yzm(){
        var obj=document.getElementById('yzm');
        obj.src='./yzm.php?type=zc&_'+Math.random();
    }
</script>
</body>
</html>